package com.breaking.second.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.breaking.second.activity.pager.adapter.NewsDetailAdapter;
import com.breaking.second.databinding.ActivityNewsDetailBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.NewsDetailEntity;
import com.breaking.second.entity.NewsEntity;
import com.breaking.second.entity.PageableEntity;
import com.breaking.second.utils.StringUtil;
import com.breaking.second.utils.compat.FormBodyCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

public class NewsDetailActivity extends BaseActivity<ActivityNewsDetailBinding> {
    private NewsEntity newsEntity;
    private final List<NewsDetailEntity> newsDetailEntities = new ArrayList<>();
    private NewsDetailAdapter newsDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        initRecycler();
        initView();
    }

    private void initView() {
        binding.send.setOnClickListener(this::send);
    }


    private void send(View view) {
        Editable message = binding.message.getText();
        if (StringUtil.isEmpty(message)) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        addChat(String.valueOf(newsEntity.getFromUserId()), message.toString());
    }

    private void initRecycler() {
        newsDetailAdapter = new NewsDetailAdapter(this, newsDetailEntities, pageableVo.getUserId());
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(newsDetailAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        if (data == null) {
            finish();
            return;
        }
        newsEntity = JSON.to(NewsEntity.class, data);
        getNewsData();
    }

    private void instLog(String chat) {
        FormBody.Builder form = new FormBody.Builder();
        form.add("chatId", String.valueOf(chat));
        OkhttpUtil.Get("chat/change", form, new OkhttpUtil.CallbackListener(new OkhttpUtil.CallbackSuccess() {
            @Override
            public void onResponse(BookEntity bookEntity) {

            }
        }));
    }

    private void addChat(String user_id, String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content", message.toString());
        jsonObject.put("toUserId", user_id.toString());
        jsonObject.put("userId", String.valueOf(pageableVo.getUserId()));
        OkhttpUtil.Post("chat", jsonObject, new OkhttpUtil.CallbackListener(this::addChat));
    }

    private void addChat(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            getNewsData();
        }
        Toast.makeText(this, bookEntity.getMsg(), Toast.LENGTH_SHORT).show();
    }

    private void getNewsData() {
        FormBody.Builder form = FormBodyCompat.to(pageableVo);
        form.add("userId", String.valueOf(pageableVo.getUserId()));
        form.add("fromUserId", String.valueOf(newsEntity.getFromUserId()));
        OkhttpUtil.Get("chat/message", form, new OkhttpUtil.CallbackListener(this::getNewsData));
    }

    private void getNewsData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            PageableEntity pageable = bookEntity.to(PageableEntity.class);

            if (pageable == null) return;
            List<NewsDetailEntity> detailEntities = pageable.toList(NewsDetailEntity.class);
            if (detailEntities == null) return;
            for (NewsDetailEntity newsDetail : detailEntities) {
                instLog(newsDetail.getId());
            }

            if (pageable.getCurrent() == 0) {
                newsDetailAdapter.notifyItemRangeRemoved(0, newsDetailEntities.size());
                newsDetailEntities.clear();
            }

            int start = this.newsDetailEntities.size();
            this.newsDetailEntities.addAll(detailEntities);
            newsDetailAdapter.notifyItemRangeChanged(start, this.newsDetailEntities.size());
        }
    }

}
